import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../models/member.dart';
import '../providers/member_provider.dart';
import 'member_item.dart';
import '../providers/auth_provider.dart';

class MemberList extends StatefulWidget {
  const MemberList({super.key});

  @override
  State<MemberList> createState() => _MemberListState();
}

class _MemberListState extends State<MemberList> {
  @override
  void initState() {
    super.initState();
    Future.microtask(() =>
        Provider.of<MemberProvider>(context, listen: false).loadMembers());
  }

  @override
  Widget build(BuildContext context) {
    final bool isAdmin = context.watch<AuthProvider>().isAdmin;
    if (!isAdmin) {
      return const Center(
        child: Text('您没有权限查看团队成员列表'),
      );
    }

    return Consumer<MemberProvider>(
      builder: (context, memberProvider, child) {
        if (memberProvider.isLoading) {
          return const Center(child: CircularProgressIndicator());
        }

        final members = memberProvider.members;
        if (members.isEmpty) {
          return const Center(child: Text('暂无团队成员'));
        }

        return ListView.builder(
          itemCount: members.length,
          itemBuilder: (context, index) {
            return MemberItem(member: members[index]);
          },
        );
      },
    );
  }
} 